﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Login : MonoBehaviour
{
    private InputField userName;
    private InputField password;

    private Button loginBtn;
    private Button registerBtn;

    private LoginRequest loginRequest;
    private RegisterRequest registerRequest;

    void Start()
    {
        loginRequest = GetComponent<LoginRequest>();
        registerRequest = GetComponent<RegisterRequest>();

        userName = this.transform.Find("UserName").GetComponent<InputField>();
        password = this.transform.Find("Password").GetComponent<InputField>();

        if (PlayerPrefs.HasKey("Username"))
        {
            userName.text = PlayerPrefs.GetString("Username");
            password.text = PlayerPrefs.GetString("Password");
        }

        loginBtn = this.transform.Find("Login").GetComponent<Button>();

        loginBtn.onClick.AddListener(() => 
        {
            //发送信息。1为OperationCode
            loginRequest.Username = userName.text;
            loginRequest.Password = password.text;
            loginRequest.DefaultRequest();
            PlayerPrefs.SetString("Username", userName.text);
            PlayerPrefs.SetString("Password", password.text);
            PlayerPrefs.Save();
        });

        registerBtn = this.transform.Find("Register").GetComponent<Button>();
        registerBtn.onClick.AddListener(delegate ()
        {
            registerRequest.Username = userName.text;
            registerRequest.Password = password.text;
            registerRequest.DefaultRequest();
        });
    }
}
